package com.example.geotoolsdemo;


import com.example.geotoolsdemo.utils.WKTUtil;
import org.geotools.data.shapefile.shp.ShapeType;
import org.geotools.data.shapefile.shp.ShapefileWriter;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GeotoolsDemoApplication {

    public static void main(String[] args) throws ParseException, IOException {
        List<String> list = new ArrayList();

        list.add("POLYGON ((116.21278950384274 39.90557982319698, 116.21177234433465 39.90610963061354, 116.21106912279264 39.90264172209895, 116.21399502548638 39.902612822554126, 116.21629305278306 39.905011479365406, 116.21278950384274 39.90557982319698))");
        list.add("POLYGON ((113.38185597038 34.54828048706,113.38224220848 34.548355588913,113.38249970055 34.548108825684,113.38237095451 34.54787279129,113.38208127594 34.547786960602,113.38185597038 34.54828048706))");

        List<Geometry> geometryList = new ArrayList();
        GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
        GeometryCollection gc = new GeometryCollection(null, gf);
        for (String str : list) {
            Geometry geom = WKTUtil.wktToGeom(str);
            geometryList.add(geom);
        }

        String url1 = "D:\\00testdata\\tmp\\myshape.shp";
        String url2 = "D:\\00testdata\\tmp\\myshape.shx";
        FileOutputStream shpFile = new FileOutputStream(url1);
        FileOutputStream shxFile = new FileOutputStream(url2);

        ShapefileWriter shapefileWriter = new ShapefileWriter(shpFile.getChannel(), shxFile.getChannel());
        shapefileWriter.write(gc, ShapeType.POLYGONZ);

    }

}
